package org.adorsys.aderp.aderplogin.client;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.StringUtils;

public class AderpBasicClientDetails implements ClientDetails {

	private static final long serialVersionUID = 1L;
	private String clientId;
	private String clientSecret;
	private Set<String> scope = Collections.emptySet();
	private Set<String> resourceIds = Collections.emptySet();
	private Set<String> authorizedGrantTypes = Collections.emptySet();
	private String registeredRedirectUri;
	private Collection<GrantedAuthority> authorities = Collections.emptySet();

	public AderpBasicClientDetails() {
	}
	public AderpBasicClientDetails(String clientId, String clientSecret, String registeredRedirectUri,  String commaSeparatedResourceIds, String commaSeparatedScopes, String commaSeparatedAuthorizedGrantTypes,
			String commaSeparatedAuthorities) {
		this(commaSeparatedResourceIds, commaSeparatedScopes, commaSeparatedAuthorizedGrantTypes, commaSeparatedAuthorities);
		this.clientId = clientId;
		this.clientSecret = clientSecret;
		this.registeredRedirectUri = registeredRedirectUri;
	}

	public AderpBasicClientDetails(String commaSeparatedResourceIds, String commaSeparatedScopes, String commaSeparatedAuthorizedGrantTypes,
			String commaSeparatedAuthorities) {

		if (StringUtils.hasText(commaSeparatedResourceIds)) {
			Set<String> resourceIds = new HashSet<String>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(commaSeparatedResourceIds)));
			if (!resourceIds.isEmpty()) {
				this.resourceIds = resourceIds;
			}
		}

		if (StringUtils.hasText(commaSeparatedScopes)) {
			Set<String> scopeList = new HashSet<String>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(commaSeparatedScopes)));
			if (!scopeList.isEmpty()) {
				this.scope = scopeList;
			}
		}

		if (StringUtils.hasText(commaSeparatedAuthorizedGrantTypes)) {
			this.authorizedGrantTypes = new HashSet<String>(Arrays.asList(StringUtils
					.commaDelimitedListToStringArray(commaSeparatedAuthorizedGrantTypes)));
		} else {
			this.authorizedGrantTypes = new HashSet<String>(Arrays.asList("authorization_code", "refresh_token"));
		}

		if (StringUtils.hasText(commaSeparatedAuthorities)) {
			this.authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(commaSeparatedAuthorities);
		}
	}
	@Override
	public String getClientId() {
		return clientId;
	}
	@Override
	public Set<String> getResourceIds() {
		return resourceIds;
	}
	@Override
	public boolean isSecretRequired() {
		return this.clientSecret != null;
	}
	@Override
	public String getClientSecret() {
		return clientSecret;
	}
	@Override
	public boolean isScoped() {
		return this.scope != null && !this.scope.isEmpty();
	}
	@Override
	public Set<String> getScope() {
		return scope;
	}
	@Override
	public Set<String> getAuthorizedGrantTypes() {
		return authorizedGrantTypes;
	}
	@Override
	public String getRegisteredRedirectUri() {
		return registeredRedirectUri;
	}
	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

}
